<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch" label-width="100px">
      <!--<el-form-item label="邮件发送人" prop="userId">-->
        <!--<el-input-->
          <!--v-model="queryParams.userId"-->
          <!--placeholder="请输入邮件发送人"-->
          <!--clearable-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="From Mail" prop="fromMail">
        <el-input
          v-model="queryParams.fromMail"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="To Mail" prop="toMail">
        <el-input
          v-model="queryParams.toMail"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="客户id" prop="candId">-->
        <!--<el-input-->
          <!--v-model="queryParams.candId"-->
          <!--placeholder="请输入客户id"-->
          <!--clearable-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="Title" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Content" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
        >Email Config</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleSendMail"
        >Send Mail</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleTemplate"
        >Create Template</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
          <!--v-hasPermi="['system:mail:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="mailList"
      @selection-change="handleSelectionChange">
      <el-table-column
        width="60px"
        label="action"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >View</el-button>
          <!--<el-button-->
          <!--size="mini"-->
          <!--type="text"-->
          <!--icon="el-icon-delete"-->
          <!--@click="handleDelete(scope.row)"-->
          <!--v-hasPermi="['system:mail:remove']"-->
          <!--&gt;删除</el-button>-->
        </template>
      </el-table-column>
      <el-table-column label="fromMail" align="left" prop="fromMail" />
      <el-table-column label="toMail" align="left" prop="toMail" />
      <el-table-column label="subject" align="left" prop="title" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="Add InterView"
      :visible.sync="openCandy"
      width="800px"
      append-to-body
    >
      <div style="display: flex;justify-content: center;margin-top: -30px;">
        <span style="color: #000;font-size: 18px;">Please Select A Candidate</span>
      </div>
      <el-form
        :model="queryParamsUser"
        ref="queryFormUser"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-top: 20px;">
        <el-form-item label="Name" prop="name">
          <el-input
            v-model="queryParamsUser.name"
            placeholder=""
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item style="display: none;">
          <el-input
            placeholder=""
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleUserQuery">Search</el-button>
        </el-form-item>
      </el-form>
      <el-table
        border
        :data="candidateList"
        style="margin-top: 0px;">
        <el-table-column width="80px;" label="action" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleAddUserCand(scope.row)"
            >Select</el-button>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="Name" align="left" prop="name" />
        <el-table-column show-overflow-tooltip label="Company" align="left" prop="ccompanyList[0].com" />
        <el-table-column show-overflow-tooltip label="Position" align="left" prop="ccompanyList[0].title" />
        <el-table-column show-overflow-tooltip label="Education" align="left" prop="schoolList[0].name" />
        <el-table-column show-overflow-tooltip width="60px" label="Age" align="left" prop="age" />
        <el-table-column show-overflow-tooltip label="salary" align="left" prop="salary" />
      </el-table>

      <pagination
        v-show="totalUser>0"
        :total="totalUser"
        :page.sync="queryParamsUser.pageNum"
        :limit.sync="queryParamsUser.pageSize"
        @pagination="getCandList"
      />
    </el-dialog>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px" append-to-body>
      <el-form ref="formConfig" :model="formConfig" :rules="rulesConfig" label-width="90px">
        <el-form-item label="Hostname" prop="hostname">
          <el-input v-model="formConfig.hostname" placeholder="" />
        </el-form-item>
        <el-form-item label="Port" prop="port">
          <el-input v-model="formConfig.port" placeholder="" />
        </el-form-item>
        <el-form-item label="Email" prop="userName">
          <el-input v-model="formConfig.userName" placeholder="" />
        </el-form-item>
        <el-form-item label="Password" prop="pwd">
          <el-input type="password" v-model="formConfig.pwd" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">Confirm</el-button>
        <el-button @click="cancel">Cancel</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMail, getMail, delMail, addMail, updateMail } from "@/api/system/mail";
import { listConf, getConf, delConf, addConf, updateConf } from "@/api/system/conf";
import { listCandidate, getCandidate, delCandidate, addCandidate, updateCandidate } from "@/api/system/candidate";

export default {
  name: "Mail",
  data() {
    return {
      candidateList: [],
      queryParamsUser: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalUser: 0,
      // 【请填写功能名称】表格数据
      mailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCandy: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        fromMail: null,
        toMail: null,
        candId: null,
        title: null,
        content: null,
        filePath: null,
        type: 0
      },
      // 表单参数
      form: {},
      confList: [],
      formConfig: {
        id: null,
        userId: null,
        hostname: null,
        port: null,
        userName: null,
        pwd: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      },
      // 表单校验
      rulesConfig: {
        hostname: [
          { required: true, message: "hostname null", trigger: "blur" }
        ],
        port: [
          { required: true, message: "port null", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "userName null", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "pwd null", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getEmailConfigList();
    this.getList();
    // this.getCandList();
  },
  methods: {
    handleAddUserCand(row){
      this.openCandy = false;
      const id = row.id || this.ids
      const obj = { path: "/system/mail/mailSend", name: "MailSend" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/mail/mailSend',query: {id: id}})
      }, 300);
    },
    handleUserQuery() {
      this.queryParamsUser.pageNum = 1;
      this.getCandList();
    },
    getCandList() {
      // this.loading = true;
      listCandidate(this.queryParamsUser).then(response => {
        this.candidateList = response.rows;
        this.totalUser = response.total;
        // this.loading = false;
      });
    },
    getEmailConfigList() {
      listConf(null).then(response => {
        this.confList = response.rows;
        if (this.confList != null && this.confList.length > 0) {
          this.formConfig = this.confList[0];
        }
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = 0;
      listMail(this.queryParams).then(response => {
        this.mailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        fromMail: null,
        toMail: null,
        candId: null,
        title: null,
        content: null,
        filePath: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleSendMail() {
      this.openCandy = true;
    },
    handleTemplate() {
      // 创建邮件模板
      const obj = { path: "/system/mail/template", name: "Template" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/mail/template'})
      }, 300);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "Email Config";
      // const obj = { path: "/system/mail/mailSend", name: "MailSend" };
      // this.$tab.closePage(obj);
      // setTimeout(()=> {
      //   this.$router.push({path:'/system/mail/mailSend', query: {type: 2}})
      // }, 300);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      const obj = { path: "/system/mail/mailSend", name: "MailSend" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/mail/mailSend',query: {id: null, mailId:id, statusT: 0}})
      }, 300);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formConfig"].validate(valid => {
        if (valid) {
          if (this.formConfig.id != null) {
            updateConf(this.formConfig).then(response => {
              this.$modal.msgSuccess("success");
              this.open = false;
              this.getEmailConfigList();
            });
          } else {
            addConf(this.formConfig).then(response => {
              this.$modal.msgSuccess("success");
              this.open = false;
              this.getEmailConfigList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delMail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mail/export', {
        ...this.queryParams
      }, `mail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
